package com.skateboard.reelviewtest.view.reelview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.skateboard.reelviewtest.R;
import com.skateboard.reelviewtest.utils.SimpleUtils;

/**
 * @作者： xuze
 * @时间： 2020/11/30 7:06 PM
 * @描述：
 */
public class ReelContainer extends RelativeLayout implements ReelLayout.ReelLayoutExpandCallback {

    private Context mContext;
    private ReelLayout reelLayout;
    private ImageView ivTypeIcon;
    private ImageView ivTypeLight;
    private final static long duration=1000;

    public ReelContainer(Context context) {
        this(context,null);
    }

    public ReelContainer(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ReelContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        init();
    }

    private void init(){
        View view = inflate(mContext, R.layout.layout_reel_container,this);
        reelLayout=view.findViewById(R.id.reel_layout);
        ivTypeIcon=view.findViewById(R.id.iv_type_icon);
        ivTypeLight=view.findViewById(R.id.iv_type_light);
        reelLayout.setExpandCallback(this);
    }


    @Override
    public void expandCallback(boolean isExpand) {
        if(isExpand){
            ObjectAnimator tranAnimator=ObjectAnimator.ofFloat(ivTypeIcon,"translationY",0, -SimpleUtils.dp2px(mContext,5))
                    .setDuration(duration);
            tranAnimator.setStartDelay(1000);
            tranAnimator.start();
            ivTypeLight.setVisibility(VISIBLE);
            ObjectAnimator scaleAnimator= ObjectAnimator.ofFloat(ivTypeLight, "scaleX", 0.5f, 5f)
                    .setDuration(1000);
            scaleAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    // 透明度动画
                    ObjectAnimator.ofFloat(ivTypeLight, "alpha", 1, 0)
                            .setDuration(1000)
                            .start();
                }
            });
            scaleAnimator.start();
        }else{
            ObjectAnimator.ofFloat(ivTypeIcon,"translationY",-SimpleUtils.dp2px(mContext,5), 0).setDuration(duration).start();
            ivTypeLight.setVisibility(GONE);
            ObjectAnimator.ofFloat(ivTypeLight, "alpha", 0, 1)
                    .setDuration(1000)
                    .start();
        }
    }
}
